//
//  DJPageController.m
//  HB
//
//  Created by 陶秋安 on 16/3/7.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "DJPageController.h"
#import "LoginOperation.h"
#import "MusicListTableViewCell.h"
#import "ZYMusicTool.h"


@implementation DJPageController

#pragma mark 设置表格有几个数据区域
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark 设置表格的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 44;
}

#pragma mark 设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _hasNextPage?musicArray.count+1:musicArray.count;
}

#pragma mark 表格绑定数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString  *cellIdentifier = @"tabMusicListCell";
    
    MusicListTableViewCell *cell = (MusicListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        
        NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
        
        cell = [Objects objectAtIndex:0];
    }
    
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    
    if(musicArray.count>0 && indexPath.row < musicArray.count)
    {
        
        NSInteger i=  indexPath.row;
        
        NSDictionary *musicInfo= [musicArray objectAtIndex:i];
        
        cell.number.text= [NSString stringWithFormat:@"%ld",(i+1)];
        
        cell.musicInfo = musicInfo;
        
        cell.lblAuthorInfo.text = [musicInfo objectForKey:@"NickName"];
        
        cell.lblMucisName.text = [musicInfo objectForKey:@"MusicName"];
        
        //cell.musicID= [musicInfo objectForKey:@"MusicID"];
        
    }
    else
    {
        
        
        NSString  *cellIdentifier = @"NewsMoreCell";
        
        MoreCell *cell = (MoreCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        pageIndex=pageIndex+1;
        
        [self loadMusicDatabByDjID] ;//加载下一页数据
        
        if (cell == nil) {
            
            NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
            
            cell = [Objects objectAtIndex:0];
            
        }
        
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        
    }
    return cell;
    
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MusicListTableViewCell *cell = (MusicListTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if(![[ZYMusicTool playingMusic].filename isEqualToString:[cell.musicInfo objectForKey:@"MusicName"]])
    {
        [ZYMusicTool playingMusic].playing=NO;
        //获取新的歌曲
        ZYMusic *currMusic=[[ZYMusic alloc]init];
        
        currMusic.name= [cell.musicInfo objectForKey:@"MusicName"];
        
        currMusic.icon= [cell.musicInfo objectForKey:@"Icon"];
        
        currMusic.iconName= [cell.musicInfo objectForKey:@"IconName"];
        
        currMusic.musicListArray = [cell.musicInfo objectForKey:@"musiclist"];
        
        currMusic.lrcname=@"";
        
        currMusic.singer=[cell.musicInfo objectForKey:@"singer"];
        
        currMusic.singerIcon=[cell.musicInfo objectForKey:@"singericon"];
        
        currMusic.djID=[cell.musicInfo objectForKey:@"ID"];
        
        currMusic.musicID=[cell.musicInfo objectForKey:@"MusicID"];
        
        currMusic.playing = YES;
        
        currMusic.shareURL= [cell.musicInfo objectForKey:@"shareURL"];
        
        [ZYMusicTool setPlayingMusic:currMusic];
        
        [self addViewRes:currMusic];
        
    }
    [self musicPlay].playMode=@"one";
    
    [self presentViewController:[self musicPlay] animated:true completion:nil];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    pageIndex=1;
    
    pageSize=10;
    
    _hasNextPage=NO;
    
    musicArray=[[NSMutableArray alloc]init];
    
    self.tableViewMusicList.delegate=self;
    
    self.tableViewMusicList.dataSource=self;
    
    //蒙版效果
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle: UIBlurEffectStyleDark];
    UIVisualEffectView *visualEffectView= [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    CGFloat width=  [UIScreen mainScreen].bounds.size.width;
    CGFloat height=self.bgImgView.bounds.size.height;
    visualEffectView.frame = CGRectMake(0.0, 0.0, width, height);
    visualEffectView.alpha = 1;
    [self.bgImgView addSubview:visualEffectView];
    self.imgIsCheck.hidden=YES;
    
    [self loadData];
    
    [self loadMusicDatabByDjID];
    
}

-(void) loadMusicDatabByDjID
{
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url= [FxGlobal getServiceUrl:@"City" andFuncName:@"MusicByDJID"];
    
    NSString *u= [NSString stringWithFormat:@"%@?djID=%@&pageIndex=%ld&pageSize=%ld", url,self.djID,pageIndex,pageSize];
    
    NSDictionary *dictInfo = @{@"url":u};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:4];
    
}

-(void) loadData
{
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url= [FxGlobal getServiceUrl:@"User" andFuncName:@"FansCount"];
    
    NSString *u= [NSString stringWithFormat:@"%@?p_djID=%@", url,self.djID];
    
    NSDictionary *dictInfo = @{@"url":u};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:0];
    
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url1= [FxGlobal getServiceUrl:@"User" andFuncName:@"MusicCount"];
    
    NSString *u1= [NSString stringWithFormat:@"%@?p_djID=%@", url1,self.djID];
    
    NSDictionary *dictInfo1 = @{@"url":u1};
    
    [operation setOpInfo:dictInfo1];
    
    [operation get:1];
    
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url2= [FxGlobal getServiceUrl:@"User" andFuncName:@"FocusCount"];
    
    NSString *u2= [NSString stringWithFormat:@"%@?p_djID=%@", url2,self.djID];
    
    NSDictionary *dictInfo2 = @{@"url":u2};
    
    [operation setOpInfo:dictInfo2];
    
    [operation get:2];
    
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url3= [FxGlobal getServiceUrl:@"User" andFuncName:@"DJInfo"];
    
    NSString *u3= [NSString stringWithFormat:@"%@?p_djID=%@", url3,self.djID];
    
    NSDictionary *dictInfo3 = @{@"url":u3};
    
    [operation setOpInfo:dictInfo3];
    
    [operation get:3];
    
}

-(void)opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    operation=nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    if (flag==0)
    {
        NSString *v=[data1 objectForKey:@"count"];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.lblFansCount.text=v;
        });

    }
    else if (flag==1)
    {
        NSString *v=[data1 objectForKey:@"count"];
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.lblMusicCount.text=v;
        });
    }
    else if (flag==2)
    {
        NSString *v=[data1 objectForKey:@"count"];
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.lblFocusCount.text=v;
        });
    }
    else  if(flag==3)
    {
        NSString *Code=[data1 objectForKey:@"Code"];
        
        NSString *HeadImg=[data1 objectForKey:@"HeadImg"];
        
        NSString *HeadImgName=[data1 objectForKey:@"HeadImgName"];
        
        NSString *isCheck=[data1 objectForKey:@"IsCheck"];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            self.lblDjCode.text=Code;
            
            self.imgHead.layer.cornerRadius=45;
            
            self.imgHead.clipsToBounds=YES;
            
            if ([isCheck isEqualToString:@"0"])
            {
                self.imgIsCheck.hidden=YES;
            }
            else
            {
                self.imgIsCheck.hidden=NO;
            }

        });
        
        if([HeadImg isEqualToString:@""])
        {
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.imgHead.image= [UIImage imageNamed:@"2.jpg"];
                self.bgImgView.image= [UIImage imageNamed:@"2.jpg"];
            });
        }
        else
        {
            [Common downLoadImageByURL:self.imgHead andURL:HeadImg andName:HeadImgName];

            [Common downLoadImageByURL:self.bgImgView andURL:HeadImg andName:HeadImgName];
        }
        
    }
    
    else if(flag==4)
    {
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSDictionary *dataParse = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        
        if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
        {
            NSInteger totleRowCount= [[dataParse objectForKey:@"totleRowCount"] integerValue];
            
            if(totleRowCount>pageIndex)
            {
                _hasNextPage=YES;
            }
            else
            {
                _hasNextPage= NO;
            }
            
            NSArray *arrayData= [dataParse objectForKey:@"data"];
            
            for (NSInteger i=0; i<[arrayData count]; i++)
            {
                
                NSDictionary *dict= [arrayData objectAtIndex:i];
                
                [musicArray addObject:dict];
            }
            
            [self.tableViewMusicList performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
        
    }
}

- (IBAction)btnBack:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
